﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Svg;
using System.Drawing;
using System.Runtime.Serialization;
using Commons.Enums;
using Svg.Transforms;
using Commons;

namespace HumanFace
{
    [DataContract]
    public class Eye : Entities.Base.EyeBASE
    {
        public Eye(NodeCollection pointList, Direction direction, Dictionary<string, string> templates)
            : base(templates, pointList)
        {
            this.EyeDirection = direction;
            CreateProperties();
            RenderVectorelGraphic();
        }
        public Eye()
        {
        }
        private void CreateEye()
        {

        }


        private void CreateProperties()
        {
            switch (EyeDirection)
            {
                case Direction.LEFT:
                    #region LeftEyeWidth
                    Node nd29 = PointList["29"];
                    Node nd27 = PointList["27"];
                    if (nd27 != null && nd29 != null)
                        this.Width = nd29.X - nd27.X;
                    #endregion
                    #region LeftEyeHeight
                    Node nd30 = PointList["30"];
                    Node nd28 = PointList["28"];
                    if (nd30 != null && nd28 != null)
                        this.Height = nd30.Y - nd28.Y;
                    #endregion
                    #region LeftStartPoint
                    if (nd29 != null)
                        StartPoint = nd29;
                    #endregion
                    #region LeftPupilLocation
                    Node nd31 = PointList["31"];
                    Node nd21 = PointList["21"];
                    Node nd26 = PointList["26"];
                    if (nd31 != null && nd21 != null && nd26 != null)
                        PupilLocation = new Node(nd31.X - nd21.X, nd31.Y - nd26.Y,"leftPupil");
                    #endregion
                    #region RightEyeUpWidth
                    Node nd37 = PointList["37"];
                    
                    if (nd37 != null && nd21 != null)
                        EyeUpWidth = nd37.X - nd21.X;
                    #endregion
                    #region LeftEyeUpHeight
                    
                    if (nd30 != null && nd26 != null)
                        EyeUpHeight = nd30.Y - nd26.Y;
                    #endregion
                    break;
                case Direction.RIGHT:
                    #region RightEyeWidth
                    Node nd32 = PointList["32"];
                    Node nd34 = PointList["34"];
                    if (nd32 != null && nd34 != null)
                        this.Width = nd32.X - nd34.X;
                    #endregion
                    #region RightEyeHeight
                    Node nd35 = PointList["35"];
                    Node nd33 = PointList["33"];
                    if (nd35 != null && nd33 != null)
                        this.Height = nd35.Y - nd33.Y;
                    #endregion
                    #region RightStartPoint
                    if (nd34 != null)
                        StartPoint = nd34;
                    #endregion
                    #region RightPupilLocation
                    Node nd36 = PointList["36"];
                    Node nd15 = PointList["15"];
                    Node nd20 = PointList["20"];
                    if (nd36 != null)
                        PupilLocation = new Node(nd15.X - nd36.X, nd36.Y - nd20.Y, "rightPupil");
                    #endregion
                    #region RightEyeUpWidth
                    Node nd45 = PointList["45"];
                    
                    if (nd45 != null && nd15 != null)
                        EyeUpWidth = nd15.X - nd45.X;
                    #endregion
                    #region RightEyeUpHeight
                    
                    if (nd35 != null && nd20 != null)
                        EyeUpHeight = nd35.Y - nd20.Y;
                    #endregion
                    
                    break;
            }

        }
        protected override void RenderVectorelGraphic()
        {
            base.RenderVectorelGraphic();
            if (this.SVG != null)
            {
                this.SVG.ID = (this.EyeDirection == Direction.LEFT) ? "leftEye" : (this.EyeDirection == Direction.RIGHT) ? "rightEye" : "EyeExeption";

                SvgTransformCollection eyeUptransformCollection = new SvgTransformCollection();
                SvgPath eyeUp = this.SVG.GetElementById<SvgPath>("eyeUp");
                if (eyeUp.Transforms != null) eyeUptransformCollection = eyeUp.Transforms;
                SketchHelper.Scale(EyeUpWidth / eyeUp.Bounds.Width, EyeUpHeight / eyeUp.Bounds.Height, eyeUptransformCollection);
                eyeUp.Transforms = eyeUptransformCollection;

                SvgTransformCollection eyeTransformCollection = new SvgTransformCollection();
                SvgGroup eye = this.SVG.GetElementById<SvgGroup>("eye");
                if (eye.Transforms != null) eyeTransformCollection = eye.Transforms;
                SvgRectangle eyeBounds = this.SVG.GetElementById<SvgRectangle>("eyeBounds");
                SketchHelper.Scale(this.Width / eyeBounds.Bounds.Width, this.Height / eyeBounds.Bounds.Height, eyeTransformCollection);
                eye.Transforms = eyeTransformCollection;

                SvgCircle eyePupil = this.SVG.GetElementById<SvgCircle>("eyePupil");
                eyePupil.CenterX = PupilLocation.X / (EyeUpWidth / eyeUp.Bounds.Width);
                eyePupil.CenterY = PupilLocation.Y / (EyeUpHeight / eyeUp.Bounds.Height);


                if (this.EyeDirection == Direction.LEFT)
                {
                    SvgTransformCollection eyeGrouptransformCollection = new SvgTransformCollection();
                    SvgGroup eyeGroup = this.SVG.GetElementById<SvgGroup>("eyeGroup");
                    if (eyeGroup.Transforms != null) eyeGrouptransformCollection = eyeGroup.Transforms;
                    SketchHelper.Translate((float)((float)this.EyeUpWidth), 0, eyeGrouptransformCollection);
                    SketchHelper.Scale(-1, 1, eyeGrouptransformCollection);
                    eyeGroup.Transforms = eyeGrouptransformCollection;
                }
            }
        }
    }
}
